package pr.iceworld.fernando.springbatchlearning.controller;

//import com.example.demo.configuration.MyBatchConfig;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import pr.iceworld.fernando.springbatchlearning.configuration.MyBatchConfig;
import pr.iceworld.fernando.springbatchlearning.entity.Users;
import pr.iceworld.fernando.springbatchlearning.repository.UserMapper;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by ferna on 2018/10/17.
 */
@RestController
public class ProcessController {

    @Resource
    private UserMapper userMapper;
    @Autowired
    private MyBatchConfig myBatchConfig;
    @RequestMapping("doProcess")
    public String doProcess() {
        int size =  userMapper.selectCount(initEntityWrapper());
        System.out.println("size ================= " + size);
        int iPage = 1;
        int pageSize = 3;
        int maxPage = size / pageSize;
        maxPage = size % pageSize > 0 ? maxPage + 1: maxPage;
        Page page = new Page(iPage, pageSize);
        while (iPage <= maxPage) {
            List<Users> list = userMapper.selectPage(page, initEntityWrapper());
            iPage ++;
            page = new Page(iPage, pageSize);
            for (Users user: list) {
                System.out.println(">>>>>>>>>>>>> " + user);
            }
        }
        myBatchConfig.run();
        return "okay";
    }

    private EntityWrapper<Users> initEntityWrapper() {
        EntityWrapper<Users> entityWrapper = new EntityWrapper<>();
        entityWrapper.le("age", 20);
        System.out.println(entityWrapper.getSqlSegment());
        return entityWrapper;
    }
}
